<?php
/**
 * User: hjd-yao
 * Date: 2018/11/6
 * Time: 16:52
 */

namespace App\Models\System;


use App\Models\BaseModel;
use Illuminate\Support\Facades\DB;

class MenuModel extends BaseModel
{
    protected $table='sys_menu';
    public $timestamps = false;

    public function getTreesMenu()
    {
        $list = self::where('status',1)->select('id','parent_id','title')->get()->toArray();
        $default =  [['value'=>'0','label'=>'一级菜单']];
        if($list){
            $default = array_merge($default,format_cascade($list));
        }
        return $default;
    }

    public function addMenu($params)
    {
        $parent_id = 0;
        $parent_ids = '';
        if(isset($params['parent_ids'])){
            $parents = $this->handleParents($params['parent_ids']);
            $parent_id = $parents['parent_id'];
            $parent_ids = $parents['parent_ids'];
        }
        $data['title'] = $params['title'];
        $data['url'] = $params['url'];
        $data['icon'] = $params['icon']?:'';
        $data['parent_id'] = $parent_id;
        $data['parent_ids'] = $parent_ids;
        $data['sort'] = $params['sort']??1000;
        $data['status'] = $params['status']??1;
        $data['comment'] = $params['comment']??$params['title'];
        $data['created_at'] = time();
        if(self::insert($data)){
            return true;
        }
        return false;
    }

    public function editMenu($params,$menu_id)
    {
        $menu = self::find($menu_id);
        $is_updated = false;
        $old_parent_ids = false;
        if(isset($params['menu_parents'])&&$menu->parent_ids!=$params['menu_parents']){
            $parents = $this->handleParents($params['menu_parents']);
            if($menu_id == $parents['parent_id']||in_array($menu_id,str2arr($parents['parent_ids']))){
                $this->errorMsg = '父级菜单不能为自身或自己的下级菜单';
                return false;
            }
            $old_parent_ids = $menu->parent_ids;
            $menu->parent_ids = $parents['parent_ids'];
            $menu->parent_id = $parents['parent_id'];
            $is_updated = true;
        }
        if(isset($params['menu_title'])&&$menu->title != $params['menu_title']){
            $menu->title = $params['menu_title'];
            $is_updated = true;
        }

        if(isset($params['menu_icon'])&&$menu->icon != $params['menu_icon']){
            $menu->icon = $params['menu_icon'];
            $is_updated = true;
        }

        if(isset($params['status'])&&$menu->status != $params['status']){
            $menu->status = $params['status'];
            $is_updated = true;
        }
        if(isset($params['sort'])&&$menu->sort != $params['sort']){
            $menu->sort = $params['sort'];
            $is_updated = true;
        }
        if(isset($params['comment'])&&$menu->comment != $params['comment']){
            $menu->comment = $params['comment'];
            $is_updated = true;
        }
        if($is_updated){
            $menu->updated_at = time();
            $menu->save();
            if($old_parent_ids !== false){
                $this->relateUpdatePids($menu_id,$menu->parent_ids,$old_parent_ids);
            }
        }
        return true;
    }

    private function handleParents($menu_parents)
    {
        $parent_id = 0;
        $parent_ids = '';
        if($menu_parents){
            $pids = array_filter(str2arr($menu_parents));
            if(is_array($pids)&&!empty($pids)){
                $parent_id = end($pids);
                $parent_ids = arr2str($pids);
            }elseif(!is_array($pids)&&!empty($pids)){
                $parent_id = $pids;
                $parent_ids = $pids;
            }
        }
        return ['parent_id'=>$parent_id,'parent_ids'=>$parent_ids];
    }

    private function relateUpdatePids($menu_id,$new_parent_ids,$old_parent_ids)
    {
        $new_parent_ids = $new_parent_ids?$new_parent_ids.','.$menu_id:$menu_id;
        $old_parent_ids = $old_parent_ids?$old_parent_ids.','.$menu_id:$menu_id;
        $sql = 'replace(`parent_ids`,"'.$old_parent_ids.'","'.$new_parent_ids.'")';
        return self::whereRaw("FIND_IN_SET(?,`parent_ids`)",[$menu_id])->update(['parent_ids'=>DB::raw($sql)]);
    }
}